﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model.Business.Interface;
using Model.Entities;

namespace Model.Business
{
    public class LogoBusiness: IBaseBusiness<Anuncio>, IDisposable
    {
        private Model.Entities.PublisinosEntities context;

        public LogoBusiness()
        {
            context = Model.Entities.SystemContext.CurrentContext;
        }

        public bool Add(Anuncio data)
        {
            data.DataAbertura = DateTime.Now;
            data.Usuario = context.Usuario.Where(u => u.UsuarioID.Equals(Framework.Security.User.UserID)).FirstOrDefault();
            context.Anuncio.AddObject(data);
            int rowsAffected = context.SaveChanges();
            return (rowsAffected > 0);
        }

        public bool Update(Anuncio data)
        {
            throw new NotImplementedException();
        }

        public bool Delete(Anuncio data)
        {
            throw new NotImplementedException();
        }

        public List<Anuncio> SelectByUser(int id)
        {
            var usuario = context.Usuario.Where(u => u.UsuarioID.Equals(Framework.Security.User.UserID)).FirstOrDefault();
            
            if (usuario != null)
                return context.Anuncio.Where(a=>a.Usuario.Equals(usuario)).ToList();
            else
                return null;
        }

        public List<Anuncio> Select()
        {
            var usuario = context.Usuario.Where(u => u.UsuarioID.Equals(Framework.Security.User.UserID)).FirstOrDefault();

            if (usuario != null)
                return context.Anuncio.Where(a => !a.Usuario.Equals(usuario)).ToList();
            else
                return context.Anuncio.ToList();
        }

        public void Dispose()
        {
            context = null;
            GC.Collect();
        }

        public Anuncio Select(int id)
        {
            return context.Anuncio.Where(a => !a.AnuncioID.Equals(id)).FirstOrDefault();
        }
    }
}
